package sfs2x.extensions.game.src;

import com.smartfoxserver.v2.db.IDBManager;
import com.smartfoxserver.v2.entities.User;
import com.smartfoxserver.v2.entities.data.ISFSArray;
import com.smartfoxserver.v2.entities.data.ISFSObject;
import com.smartfoxserver.v2.entities.data.SFSObject;
import com.smartfoxserver.v2.extensions.BaseClientRequestHandler;

public class Gemiler extends BaseClientRequestHandler {
    
	 public void handleClientRequest(User arg0, ISFSObject params)
	  {
		 IDBManager dbManager = getParentExtension().getParentZone().getDBManager();
	     ISFSObject rtn = new SFSObject();
	     String statement = null;
	     ISFSObject abk = new SFSObject();
	     try{
	    	 
	    	 int d = params.getInt("deger");
	         statement = "INSERT INTO gemi ('Tip','Konum') Values("+d+")";
	         dbManager.executeUpdate(statement);
	         int g = params.getInt("gemisayisi");
			 statement = "UPDATE kullanici SET Gemi_sayisi = Gemi_sayisi+1 where Login_isim = '"+arg0.getName()+"'";
			 
   		     statement = ("SELECT x,y,z,idGemi,COALESCE(Kullanici_id,0) FROM konum k, gemi g"
   				  +" left join (kullanicigemi kg) on Gemi_id = idGemi where g.konum = k.idKonum");
   		     ISFSArray res1 = dbManager.executeQuery(statement);
   		     
   		     int size1 = res1.size();
   		  
   		     for(int i=0;i<size1;i++){
   			  abk = res1.getSFSObject(i);
   			  float x = abk.getDouble("x").floatValue();
   			  float y = abk.getDouble("y").floatValue();
   			  float z = abk.getDouble("z").floatValue();
   			  int kullaniciid = abk.getInt("Kullanici_id");
   			  int gemiid = abk.getInt("idGemi");
   			  trace(x,y,z);
   			  rtn.putFloat("Gemi konum x"+i, x);
   			  rtn.putFloat("Gemi konum y"+i, y);
   			  rtn.putFloat("Gemi konum z"+i, z);
   			  rtn.putInt("Kullanici id"+i,kullaniciid);
   			  rtn.putInt("Gemi id"+i, gemiid);
   		  }

	     }
		 catch(Exception e)
		 {
			  e.printStackTrace();
			  trace("SQL Error: Statement = "+ statement); 
		 }

		 	GameExtension parentEx = (GameExtension) getParentExtension();
		    parentEx.send("GemilerCevap", rtn, arg0);
		
	       
	  }
}
